///////////////////////////////////////////////////
// Election Administration Polarization
// Make RD Table for Balance on Covariates
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the demographic data, list of counties out of score, and the list of counties with election data
use "$path/analysis_data/county_demographics.dta", clear
keep if policy_year==2010
merge 1:1 state county using "$path/analysis_data/counties_not_in_scope.dta", keep(1 3) nogen
merge 1:1 state county using "$path/analysis_data/county_map_data.dta", keep(1 3) nogen

* Flag the counties in and out of the sample
gen in_sample = races!=.

* Get a population variable in thousands
gen pop1k = pop/1000

* Store the mean and standard deviation for our county characteristics
matrix define X = J(8, 9, .)
local i = 1
foreach v in pop1k share_nhwhite share_black share_hisp region_ne region_mw region_s region_w {
	sum `v' if in_sample==1 & not_in_scope==.
	matrix X[`i',1] = r(mean)
	matrix X[`i',2] = r(sd)
	matrix X[`i',3] = r(N)
	sum `v' if in_sample==0 & not_in_scope==.
	matrix X[`i',4] = r(mean)
	matrix X[`i',5] = r(sd)
	matrix X[`i',6] = r(N)
	sum `v' if not_in_scope==1
	matrix X[`i',7] = r(mean)
	matrix X[`i',8] = r(sd)
	matrix X[`i',9] = r(N)
	local i = `i' + 1
}

* Make the table
qui {
cap log close
set linesize 255
log using "$path/output/sample_descriptives.tex", text replace
noi di "\begin{table}[ht]"
noi di "\centering"
noi di "\caption{\textbf{Description of Counties In and Not In Sample.}"
noi di "\label{tab:sample_descriptives}}"
noi di "\begin{tabular}{lccc}"
noi di "\toprule \toprule"
noi di " Outcome & In Sample & Not In Sample & Not In Scope \\"
noi di "\midrule"
noi di "Population (Thousands) & " %4.2f X[1,1] " & " %4.2f X[1,4] " & " %4.2f X[1,7] "\\"
noi di " & (" %4.2f X[1,2] ") & (" %4.2f X[1,5] ") & (" %4.2f X[1,8] ") \\[2mm]"
noi di "Share Non-Hispanic White & " %4.2f X[2,1] " & " %4.2f X[2,4] " & " %4.2f X[2,7] "\\"
noi di " & (" %4.2f X[2,2] ") & (" %4.2f X[2,5] ") & (" %4.2f X[1,8] ") \\[2mm]"
noi di "Share Black & " %4.2f X[3,1] " & " %4.2f X[3,4] " & " %4.2f X[3,7] "\\"
noi di " & (" %4.2f X[3,2] ") & (" %4.2f X[3,5] ") & (" %4.2f X[3,8] ") \\[2mm]"
noi di "Share Hispanic & " %4.2f X[4,1] " & " %4.2f X[4,4] " & " %4.2f X[4,7] "\\"
noi di " & (" %4.2f X[4,2] ") & (" %4.2f X[4,5] ") & (" %4.2f X[4,8] ") \\[2mm]"
noi di "Northeast & " %4.2f X[5,1] " & " %4.2f X[5,4] " & " %4.2f X[5,7] "\\[2mm]"
noi di "Midwest & " %4.2f X[6,1] " & " %4.2f X[6,4] " & " %4.2f X[6,7] "\\[2mm]"
noi di "South & " %4.2f X[7,1] " & " %4.2f X[7,4] " & " %4.2f X[7,7] "\\[2mm]"
noi di "West & " %4.2f X[8,1] " & " %4.2f X[8,4] " & " %4.2f X[8,7] "\\[2mm]"
noi di "Num Counties & " %6.0fc X[1,3] " & " %6.0fc X[1,6] " & " %6.0fc X[1,9] " \\ "
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{3}{p{0.6\textwidth}}{\footnotesize Standard deviations reported in parentheses below group means.}"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}

